<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }


        #box {
            width: 500px;
            height: 400px;
            background-color: #5fafcc;
            /* border-radius: 50%; */
            position: relative;
            margin: auto;
            margin-top: 5em;


        }

        #son {
            width: 70px;
            height: 70px;
            background-image: linear-gradient(#87def8, #63bbbb);
            border-radius: 50%;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="box">
        <div id="son"></div>
    </div>
    <script>
        let box = document.getElementById('box');
        let son = document.getElementById('son');
        let i, j;
        son.onmousedown = function (e) {
            i = e.offsetX;
            j = e.offsetY;
            console.log(e.offsetX, e.offsetY);
            box.onmousemove = function (event) {
                let x = event.clientX - i - box.offsetLeft;
                console.log(event.clientX);
                let y = event.clientY - j - box.offsetTop;
                if (x < 0) {
                    x = 0
                }
                if (x > box.clientWidth- son.clientWidth) {
                    x = box.offsetWidth - son.offsetWidth
                }
                if (y < 0) {
                    y = 0
                }
                if (y > box.clientHeight - son.clientHeight) {
                    y = box.clientHeight - son.clientHeight
                }
                console.log(event.screenX - i, event.screenY - j, son);
                son.style.cssText = `
                left:${x}px;
                top:${y}px;
                `
            }
            box.onmouseup = function (event) {
                box.onmousemove = null;

            }

        }
    </script>
</body>

</html>